###### This file cleans the raw data from the Survey Experiment and produces #######
###### the file needed for Figure 3, Table 3, and Table G.3.                 #######

library(reshape)
library(ggplot2)
library(car)
library(foreign)
library(xtable)
library(stargazer)
library(psych)


stderr <- function(x) sqrt(var(x)/length(x))

data1 <-read.csv("ExperimentalDataRaw.csv", header=TRUE, sep=",")

### Removing Individuals who are too slow or too fast
data1 <- data1[which(data1$Q_TotalDuration>336), ] ##cut out speeders
data1 <- data1[which(data1$Q_TotalDuration<1993), ] ##cut out slow people



### Creating treatment variables
data1$treat[data1$t_control_1==1] <- 1
data1$treat[data1$t_security_1==1] <- 2
data1$treat[data1$t_hr_1==1] <- 3
data1$treat[data1$t_hr_1.1==1] <-4
data1$treat[data1$t_imm_1==1] <-5
data1$treat[data1$t_dom_1==1] <-6

data1$treat1<-data1$treat2<- data1$treat3 <-data1$treat4 <- data1$treat5 <- data1$treat6 <- 0
data1$treat1[data1$t_control_1==1] <- 1
data1$treat2[data1$t_security_1==1] <- 1
data1$treat3[data1$t_hr_1==1] <- 1
data1$treat4[data1$t_hr_1.1==1] <-1
data1$treat5[data1$t_imm_1==1] <-1
data1$treat6[data1$t_dom_1==1] <-1

### Controls: 

##REpublicans are positive, Democrats are negative
data1$pid7[data1$pid==1 & data1$pid_strong==1] <- 3
data1$pid7[data1$pid==1 & data1$pid_strong==0] <- 2
data1$pid7[data1$pid==2 & data1$pid_strong==1] <- -3
data1$pid7[data1$pid==2 & data1$pid_strong==0] <- -2
data1$pid7[data1$pid_lean==1] <- 1
data1$pid7[data1$pid_lean==-1] <- -1
data1$pid7[ data1$pid_lean==0] <- 0


###Based on T/F
data1$tfc_1 <- recode(data1$tf_1, "1=0;2=1")
data1$tfc_2 <- recode(data1$tf_2, "1=0;2=1")
data1$tfc_3 <- recode(data1$tf_3, "1=0;2=1")
data1$tfc_4 <- recode(data1$tf_4, "1=0;2=1")
data1$tfc_5 <- recode(data1$tf_5, "1=1;2=0")
data1$tfc_6 <- recode(data1$tf_6, "1=0;2=1")
data1$tfc_7 <- recode(data1$tf_7, "1=1;2=0")
data1$tfc_8 <- recode(data1$tf_8, "1=0;2=1")
data1$tfc_9 <- recode(data1$tf_9, "1=0;2=1")

data1$tfc_tot <- rowSums(subset(data1, select=175:184))/9



###manipulation check
#hr, immigration, lr, national security, womens, other

data1$man_hr <- 0
data1$man_hr[data1$ht_man1==1] <- 1

data1$man_imm <- 0
data1$man_imm[data1$ht_man1==2] <- 1

data1$man_labor <- 0
data1$man_labor[data1$ht_man1==3] <- 1

data1$man_sec <- 0
data1$man_sec[data1$ht_man1==4] <- 1

data1$man_women <- 0
data1$man_women[data1$ht_man1==5] <- 1

sec <- t.test(data1$man_sec[data1$treat==1],data1$man_sec[data1$treat!=1])
hr <- t.test(data1$man_hr[data1$treat==2],data1$man_hr[data1$treat!=2])
labor <- t.test(data1$man_labor[data1$treat==2],data1$man_labor[data1$treat!=2])
women <- t.test(data1$man_women[data1$treat==3],data1$man_women[data1$treat!=3])
imm <- t.test(data1$man_imm[data1$treat==4],data1$man_imm[data1$treat!=4])

data1$ht_man22 <- recode(data1$ht_man2, "3=0")
local <- t.test(data1$ht_man22[data1$treat==5], data1$ht_man22[data1$treat!=5])

df.man <- data.frame(
  position = as.factor(c(1:12)),
  treated = as.factor(c(rep(c(0,1),6))),
  group = as.factor(c(1,1,2,2,3,3,4,4,5,5,6,6)),
  mean = c(sec$estimate[1], sec$estimate[2], hr$estimate[1], hr$estimate[2],
           labor$estimate[1],labor$estimate[2], women$estimate[1], women$estimate[2],
           imm$estimate[1],imm$estimate[2], local$estimate[1]-1, local$estimate[2]-1),
  se = c(stderr(data1$man_sec[data1$treat==1]), stderr(data1$man_sec[data1$treat!=1]),
         stderr(data1$man_hr[data1$treat==2]), stderr(data1$man_hr[data1$treat!=2]),
         stderr(data1$man_labor[data1$treat==2]), stderr(data1$man_labor[data1$treat!=2]),
         stderr(data1$man_women[data1$treat==3]), stderr(data1$man_women[data1$treat!=3]),
         stderr(data1$man_imm[data1$treat==4]), stderr(data1$man_imm[data1$treat!=4]),
         stderr(data1$ht_man22[data1$treat==5]), stderr(data1$ht_man22[data1$treat==5]))
  )

ggplot(df.man, aes(x=group, y=mean, fill=treated)) + 
  #geom_hline(yintercept=4, linetype="dotted", colour="black") +
  #coord_cartesian(ylim=c(2.7,5.3)) +
  geom_bar(position=position_dodge(), stat="identity",
           colour="black", # Use black outlines,
           size=.3, width=.6) +      # Thinner lines
  geom_errorbar(aes(ymin=mean-(1.96*se), ymax=mean+(1.96*se)),
                size=.3,    # Thinner lines
                width=.2,
                position=position_dodge(.9)) +
  scale_x_discrete(name="\n Treatments", 
                   breaks=c("1", "2","3","4","5","6"), 
                   labels=c("Security","HR","Labor", "WR","Immigration","Local")) +
  #   ylab("Mean Ranking of Statement as a Promise \n") +
     scale_fill_manual(name="Statement Type", breaks = c(0,1), 
                       labels=c("Treated","Not Treated"),
                       values =c("grey70","white")) +
  theme_bw() #+ theme(legend.position="none")


#concern, problem, govt_ht, recode
data1$white <- 0
data1$white[data1$race==1] <- 1

data1$ht_govtR <- (data1$ht_govt -1)/4



######TRUE FALSE clean up#############
data1$tfc_per <- 100*data1$tfc_tot

#data1$tfc_sum <- rowSums(subset(data1, select=169:177))
data1$tfc_sum <- rowSums(subset(data1, select=175:183))

data1$tfc_sum0<- data1$tfc_sum1 <- data1$tfc_sum2 <- data1$tfc_sum3 <- data1$tfc_sum4 <- data1$tfc_sum5 <- data1$tfc_sum6 <- data1$tfc_sum7 <- data1$tfc_sum8 <- data1$tfc_sum9 <- 0
data1$tfc_sum0[data1$tfc_sum==0]<-1
data1$tfc_sum1[data1$tfc_sum==1]<-1
data1$tfc_sum2[data1$tfc_sum==2]<-1
data1$tfc_sum3[data1$tfc_sum==3]<-1
data1$tfc_sum4[data1$tfc_sum==4]<-1
data1$tfc_sum5[data1$tfc_sum==5]<-1
data1$tfc_sum6[data1$tfc_sum==6]<-1
data1$tfc_sum7[data1$tfc_sum==7]<-1
data1$tfc_sum8[data1$tfc_sum==8]<-1
data1$tfc_sum9[data1$tfc_sum==9]<-1



###############SUmmary Table ###########
t <- function(x) {c(length(x), mean(x), sd(x),
                    min(x), max(x))}
s <- function(x) {c(mean(x), sd(x),
                         min(x), max(x))}


concern <- c(length(data1$concern), mean(data1$concern), sd(data1$concern),
             min(data1$concern), max(data1$concern))

problem <- c(length(data1$problem), mean(data1$problem), sd(data1$problem),
             min(data1$problem), max(data1$problem))

ht_govt <- t(data1$ht_govt)

age <- t(data1$age)
data1$female <- 1-data1$male
female <- t(data1$female)
income <- t(data1$income)
data1$white <- recode(data1$race, "1=1;2=0;3=0;4=0;5=0;6=0;7=0;8=0")
white <- t(data1$white)
pid <- t(data1$pid7)

demos <- rbind(concern, problem, ht_govt, age, female, income, white, pid)
xtable(demos)




#######Indices ###########3

#policy
data1$govt_construct <- (data1$govt_grid_2+data1$govt_grid_3+data1$govt_grid_4 + data1$govt_grid_5+data1$govt_grid_6+data1$govt_grid_8)/6
data1$govt_constructR<- (data1$govt_construct-1)/4

data1$govt_construct <- (data1$govt_grid_3 + data1$govt_grid_5+data1$govt_grid_6)/3
data1$govt_constructR<- (data1$govt_construct-1)/4

data1$govt_constructAll <- (data1$govt_grid_2+data1$govt_grid_3+data1$govt_grid_4 + data1$govt_grid_5+data1$govt_grid_6+data1$govt_grid_8+ data1$govt_grid_1+data1$govt_grid_7)/8
data1$govt_constructAllR <- (data1$govt_constructAll-1)/4

data1$govt_construct2 <- (data1$govt_grid_2+data1$govt_grid_3+data1$govt_grid_4 + data1$govt_grid_5+data1$govt_grid_6+data1$govt_grid_7)/8

data1$vict_construct <- (data1$ht_vict_1 + data1$ht_vict_2+ data1$ht_vict_3+ data1$ht_vict_4+data1$ht_vict_5+ data1$ht_vict_6+ data1$ht_vict_7)/6
data1$vict_constructR <- (data1$vict_construct-1)/4


data1$behavior_construct <- (data1$behavior_1 + data1$behavior_2 + data1$behavior_3 + data1$behavior_4 + data1$behavior_5 + data1$behavior_6 + data1$behavior_7)/7
data1$behavior_constructR <- (data1$behavior_construct-1)/4

save(data1, file = "Experimental_Output.RData")



